package com.finapps.stopspend.util;

import java.util.Calendar;

public class CalendarUtils {
	
	public static String calendar2Data(Calendar calendar) {
		String r=""+calendar.get(Calendar.YEAR);
		String smonth=""+(calendar.get(Calendar.MONTH)+1);
		if(smonth.length()==1)
			smonth="0"+smonth;
		String sday=""+(calendar.get(Calendar.DAY_OF_MONTH)+1);
		if(sday.length()==1)
			sday="0"+sday;
		return r+smonth+sday;
	}
	
	public static String calendar2Data2(Calendar calendar) {
		String r=""+calendar.get(Calendar.YEAR);
		String smonth=""+(calendar.get(Calendar.MONTH)+1);
		if(smonth.length()==1)
			smonth="0"+smonth;
		String sday=""+(calendar.get(Calendar.DAY_OF_MONTH)+1);
		if(sday.length()==1)
			sday="0"+sday;
		return sday+"/"+smonth+"/"+r;
	}
	
	public static String calendar2Hora(Calendar calendar) {
		String shour=""+calendar.get(Calendar.HOUR);
		if(shour.length()==1)
			shour="0"+shour;
		String sminute=""+calendar.get(Calendar.MINUTE);
		if(sminute.length()==1)
			sminute="0"+sminute;
		return shour+sminute;
	}
	
	public static Calendar data2Calendar(String  data) {
		Calendar calendar=Calendar.getInstance();
		calendar.set(Calendar.YEAR,Integer.parseInt(data.substring(0, 4)));
		calendar.set(Calendar.MONTH,Integer.parseInt(data.substring(4,6))-1);
		calendar.set(Calendar.DAY_OF_MONTH,Integer.parseInt(data.substring(6,8)));
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar;
	}
	
	public static Calendar data2Calendar(String dia, String mes, String any) {
		Calendar calendar=Calendar.getInstance();
		calendar.set(Calendar.YEAR,Integer.parseInt(any));
		calendar.set(Calendar.MONTH,Integer.parseInt(mes));
		calendar.set(Calendar.DAY_OF_MONTH,Integer.parseInt(dia));
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar;
	}
	
	public static void timeLessCalendar(Calendar calendar) {
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		calendar.set(Calendar.MILLISECOND,0);
	}
	
    /**
     * Diferencia entre dos dates
     * @return Per dues dates iguals retorna 0
     */
    public static int daysBetween(Calendar d1,Calendar d2) {
    	if(!CalendarUtils.isTimeLess(d1)) {
    		d1=(Calendar)d1.clone();
    		CalendarUtils.timeLessCalendar(d1);
    	}
    	if(!CalendarUtils.isTimeLess(d2)) {
    		d2=(Calendar)d2.clone();
    		CalendarUtils.timeLessCalendar(d2);
    	}
    	if(!d1.after(d2)) {
        	if(d1.get(Calendar.YEAR)!=d2.get(Calendar.YEAR)) {
        		Calendar d1copy=(Calendar)d1.clone();
        		d1copy.set(Calendar.DAY_OF_YEAR,d1copy.getActualMaximum(Calendar.DAY_OF_YEAR));
        		int thisyear=daysBetween(d1,d1copy);

        		d1copy.add(Calendar.YEAR,1);
        		d1copy.set(Calendar.DAY_OF_YEAR,1);
        		return thisyear+daysBetween(d1copy, d2);
        	} else {
        		int c2=d2.get(Calendar.DAY_OF_YEAR);
            	int c1=d1.get(Calendar.DAY_OF_YEAR);
            	return c2-c1;
        	}
    	} else {
    		return -1*daysBetween(d2, d1);
    	}
    }	
    
    public static boolean isTimeLess(Calendar cal) {
    	return cal.get(Calendar.HOUR_OF_DAY)==0 &&
    		cal.get(Calendar.MINUTE)==0 &&
    		cal.get(Calendar.SECOND)==0 &&
    		cal.get(Calendar.MILLISECOND)==0;
    }
}
